<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./template/DefaultTemplate.xhtml">

    <ui:define name="top">
        <title>Email</title>
    </ui:define>

    <ui:define name="left">
        left
    </ui:define>

    <ui:define name="content">
        <f:view contentType="text/html">
            <p:tabView effect="opacity" effectDuration="normal" dynamic="true" cache="false" tabChangeListener="#{email.action}"
                       style="width:800px;height:980px"> 

                <p:tab title="Create Mail" id ="create"> 
                    <h:form>
                        <h1>Create Email Message</h1>
                        <p:messages />
                        <h:panelGrid columns="2">
                            From:
                            <p:inputText id ="email" value="#{email.sender}" readonly="true" style="width:630px"/> 

                            <p:commandButton value="To:" style="height:28px;width:90px;text-align:left" onclick="address.show();" type="button" immediate="true"/>
                            <p:inputText id="reciever" value="#{email.to}" style="width:630px" >
                            </p:inputText>

                            <p:commandButton value="Cc:" style="height:28px;width:90px;text-align:left" onclick="addressCc.show();" type="button" immediate="true"/>
                            <p:inputText id="cc" value="#{email.cc}" style="width:630px"> 
                            </p:inputText>

                            Subject:
                            <p:inputText id="subject" value="#{email.subject}" style="width:630px" />
                            Message:
                            <p:editor id ="content" value="#{email.msgText}" widgetVar="editor" width="630" height="480" />  
                            Attachments:
                            <h:form enctype="multipart/form-data">
                                <p:fileUpload fileUploadListener="#{email.handleFileUpload}"   
                                              allowTypes="*.jpg;*.png;*.gif;" description="Images"/>

                            </h:form>
                        </h:panelGrid>
                        <p:commandButton ajax="false" value="Send" actionListener ="#{email.sendMail}"/>
                        <p:commandButton ajax="false" value="Save as Draft" actionListener ="#{email.saveDraft}" />
                        <p:dialog header="Address Book" widgetVar="address" modal="true" height="345" width="600">  
                            <h:panelGrid columns="2">
                                <p:pickList value="#{contactEntityController.addressBook}" var="address" itemLabel="#{address}" itemValue="#{address}" >
                                    <f:facet name="sourceCaption">System Users</f:facet>  
                                    <f:facet name="targetCaption">Recipients</f:facet>
                                </p:pickList>
                                <br />
                                <p:commandButton ajax="false" value="Add Recipients" action ="#{email.addFromAddressBook}"/>
                            </h:panelGrid>
                        </p:dialog> 
                        <p:dialog header="Address Book" widgetVar="addressCc" modal="true" height="345" width="600">  
                            <h:panelGrid columns="2">
                                <p:pickList value="#{contactEntityController.addressBookCc}" var="addressCc" itemLabel="#{addressCc}" itemValue="#{addressCc}" >
                                    <f:facet name="sourceCaption">System Users</f:facet>  
                                    <f:facet name="targetCaption">Recipients</f:facet>
                                </p:pickList>
                                <br />
                                <p:commandButton ajax="false" value="Add Recipients" action ="#{email.addFromAddressBookCc}"/>
                            </h:panelGrid>
                        </p:dialog>
                    </h:form>
                </p:tab> 

                <p:tab title="Inbox" id ="inbox">
                    <h:form id="inboxView">
                        <p:dataTable  value="#{email.msg}" var="message"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{email.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{email.messageSelect}">

                            <p:column filterBy="#{message.subject}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                                <h:outputText value="#{message.subject}" /> </p:column>

                            <p:column filterBy="#{(message.from)[0].toString()}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="From" /> </f:facet>
                                <h:outputText value="#{(message.from)[0].toString()}" /> </p:column>

                            <p:column filterBy="#{message.sentDate.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Received" /> </f:facet>
                                <h:outputText value="#{message.sentDate.toString()}" /> </p:column>

                            <p:column style="width:32px">  
                                <p:commandButton value="Delete" ajax="false" actionListener="#{email.inboxDeleteMessages}" >  
                                    <f:setPropertyActionListener value="#{message}" target="#{email.selectedMessage}" />  
                                </p:commandButton>  
                            </p:column>

                        </p:dataTable> 
                    </h:form>
                </p:tab>

                <p:tab title="Sent Mails" id ="sent">
                    <h:form>
                        <p:dataTable  value="#{email.msg}" var="message"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{email.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{email.messageSelect}" >

                            <p:column filterBy="#{message.subject}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                                <h:outputText value="#{message.subject}" /> </p:column>
                            <p:column filterBy="#{(message.allRecipients)[0].toString()}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="To" /> </f:facet>
                                <h:outputText value="#{(message.allRecipients)[0].toString()}" /> </p:column>
                            <p:column filterBy="#{message.sentDate.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Sent" /> </f:facet>
                                <h:outputText value="#{message.sentDate.toString()}" /> </p:column>

                            <p:column style="width:32px">  
                                <p:commandButton value="Delete" ajax="false" actionListener="#{email.sentDeleteMessages}" >  
                                    <f:setPropertyActionListener value="#{message}" target="#{email.selectedMessage}" />  
                                </p:commandButton>  
                            </p:column>

                        </p:dataTable>  
                    </h:form>
                </p:tab>

                <p:tab title="Drafts" id ="drafts">
                    <h:form>
                        <p:dataTable  value="#{email.msg}" var="message"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{email.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{email.draftSelect}" >

                            <p:column filterBy="#{message.subject}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                                <h:outputText value="#{message.subject}" /> </p:column>
                            <p:column filterBy="#{(message.allRecipients)[0].toString()}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="To" /> </f:facet>
                                <h:outputText value="#{(message.allRecipients)[0].toString()}" /> </p:column>
                            <p:column filterBy="#{message.sentDate.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Created" /> </f:facet>
                                <h:outputText value="#{message.sentDate.toString()}" /> </p:column>

                            <p:column style="width:32px">  
                                <p:commandButton value="Delete" ajax="false" actionListener="#{email.draftsDeleteMessages}" >  
                                    <f:setPropertyActionListener value="#{message}" target="#{email.selectedMessage}" />  
                                </p:commandButton>  
                            </p:column>

                        </p:dataTable>   
                    </h:form>
                </p:tab>

                <p:tab title="Trash" id ="trash">
                    <h:form>
                        <p:dataTable  value="#{email.msg}" var="message"
                                      paginator="true" rows="10"  
                                      paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                                      selection="#{email.selectedMessage}" selectionMode="single"
                                      rowSelectListener="#{email.messageSelect}" >

                            <p:column filterBy="#{message.subject}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="Subject" /> </f:facet>
                                <h:outputText value="#{message.subject}" /> </p:column>
                            <p:column filterBy="#{(message.from)[0].toString()}" filterMatchMode="contains"> <f:facet name="header">
                                    <h:outputText value="From" /> </f:facet>
                                <h:outputText value="#{(message.from)[0].toString()}" /> </p:column>
                            <p:column filterBy="#{message.sentDate.toString()}"> <f:facet name="header">
                                    <h:outputText value="Date Sent" /> </f:facet>
                                <h:outputText value="#{message.sentDate.toString()}" /> </p:column>
                        </p:dataTable> 
                    </h:form>
                </p:tab>
            </p:tabView>
        </f:view>
    </ui:define>

</ui:composition>
